﻿using BLL;
using Entity.Models.Entities;
using IBLL;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using RepositorySys.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using utils;

namespace RepositorySys.Controllers
{
    public class LoginController : Controller
    {
        public IActionResult Index()
        {
            return View();
        }

        ILoginBLL loginBLL;

        public LoginController(ILoginBLL loginBLL)
        {
            this.loginBLL = loginBLL;
        }



        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="userInfoDTO"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult Login(UserInfo userInfo)
        {
            string mgs;
            string userName;
            string userId;
            string userImg;
            bool login= loginBLL.Login(new UserInfoDTO(userInfo.Account, userInfo.PassWord), out mgs,out userName, out userId, out userImg);
            if (login)
            {
                HttpContext.Session.SetString("userName", userName);
                HttpContext.Session.SetString("userId", userId);
            }
            return Json(new JsonResults(200, mgs, new { userName , userImg }));
        }

        /// <summary>
        /// 退出登录
        /// </summary>
        /// <returns></returns>
        public IActionResult DropLogin()
        {
            Obliterate();
            return Json(new JsonResults(200, "退出登录成功", true));
        }

        /// <summary>
        /// 清除服务器上的Session
        /// </summary>
        public void Obliterate()
        {
            HttpContext.Session.Remove("userId");
            HttpContext.Session.Remove("userName");
        }
    }
}
